package tools.packet;

import client.*;
import client.inventory.*;
import constants.GameConstants;
import server.MapleItemInformationProvider;
import server.movement.LifeMovementFragment;
import server.shops.AbstractPlayerStore;
import server.shops.IMaplePlayerShop;
import tools.KoreanDateUtil;
import tools.Pair;
import tools.data.MaplePacketLittleEndianWriter;

import java.util.*;
import java.util.Map.Entry;

public class PacketHelper {
    private static long FT_UT_OFFSET = 116444592000000000L;
    public static long MAX_TIME = 150842304000000000L;
    public static byte[] unk1;
    public static byte[] unk2;

    public static long getKoreanTimestamp(long realTimestamp) {
        if (realTimestamp == -1L) {
            return 150842304000000000L;
        }
        long time = realTimestamp / 1000L / 60L;
        return time * 600000000L + 116444592000000000L;
    }

    public static long getTime(long realTimestamp) {
        if (realTimestamp == -1L) {
            return 150842304000000000L;
        }
        long time = realTimestamp / 1000L;
        return time * 10000000L + 116444592000000000L;
    }

    public static long getFileTimestamp(long timeStampinMillis, boolean roundToMinutes) {
        if (TimeZone.getDefault().inDaylightTime(new Date())) {
            timeStampinMillis -= 3600000L;
        }
        long time;
        if (roundToMinutes) {
            time = timeStampinMillis / 1000L / 60L * 600000000L;
        } else {
            time = timeStampinMillis * 10000L;
        }
        return time + 116444592000000000L;
    }

    public static void addImageInfo(MaplePacketLittleEndianWriter mplew, byte[] image) {
        mplew.writeInt(image.length);
        mplew.write(image);
    }

    public static void addQuestInfo(MaplePacketLittleEndianWriter mplew, MapleCharacter chr) {
        List<MapleQuestStatus> started = chr.getStartedQuests();
        mplew.writeShort(started.size());
        for (MapleQuestStatus q : started) {
            mplew.writeShort(q.getQuest().getId());
            mplew.writeMapleAsciiString((q.getCustomData() != null) ? q.getCustomData() : "");
        }
        List<MapleQuestStatus> completed = chr.getCompletedQuests();
        mplew.writeShort(completed.size());
        for (MapleQuestStatus q2 : completed) {
            mplew.writeShort(q2.getQuest().getId());
            int time = KoreanDateUtil.getQuestTimestamp(q2.getCompletionTime());
            mplew.writeInt(time);
            mplew.writeInt(time);
        }
    }

    public static void addSkillInfo(MaplePacketLittleEndianWriter mplew, MapleCharacter chr) {
        Map<ISkill, SkillEntry> skills = chr.getSkills();
        mplew.writeShort(skills.size());
        for (Entry<ISkill, SkillEntry> skill : skills.entrySet()) {
            mplew.writeInt(((ISkill) skill.getKey()).getId());
            if (((ISkill) skill.getKey()).getId() != 12) {
                mplew.writeInt((int) ((SkillEntry) skill.getValue()).skillevel);
            } else {
                mplew.writeInt(0);
            }
            if (((ISkill) skill.getKey()).isFourthJob()) {
                mplew.writeInt((int) ((SkillEntry) skill.getValue()).masterlevel);
            }
        }
    }

    public static void addCoolDownInfo(MaplePacketLittleEndianWriter mplew, MapleCharacter chr) {
        List<MapleCoolDownValueHolder> cd = chr.getCooldowns();
        mplew.writeShort(cd.size());
        for (MapleCoolDownValueHolder cooling : cd) {
            mplew.writeInt(cooling.skillId);
            mplew.writeShort((int) (cooling.length + cooling.startTime - System.currentTimeMillis()) / 1000);
        }
    }

    public static void addRocksInfo(MaplePacketLittleEndianWriter mplew, MapleCharacter chr) {
        int[] mapz = chr.getRegRocks();
        for (int i = 0; i < 5; ++i) {
            mplew.writeInt(mapz[i]);
        }
        int[] map = chr.getRocks();
        for (int j = 0; j < 10; ++j) {
            mplew.writeInt(map[j]);
        }
    }

    public static void addMonsterBookInfo(MaplePacketLittleEndianWriter mplew, MapleCharacter chr) {
        mplew.writeInt(chr.getMonsterBookCover());
        mplew.write(0);
        chr.getMonsterBook().addCardPacket(mplew);
    }

    public static void addRingInfo(MaplePacketLittleEndianWriter mplew, MapleCharacter chr) {
        mplew.writeShort(0);
        Pair<List<MapleRing>, List<MapleRing>> aRing = chr.getRings(true);
        List<MapleRing> cRing = aRing.getLeft();
        mplew.writeShort(cRing.size());
        for (MapleRing ring : cRing) {
            mplew.writeInt(ring.getPartnerChrId());
            mplew.writeAsciiString(ring.getPartnerName(), 13);
            mplew.writeLong((long) ring.getRingId());
            mplew.writeLong((long) ring.getPartnerRingId());
        }
        List<MapleRing> fRing = aRing.getRight();
        mplew.writeShort(fRing.size());
        for (MapleRing ring2 : fRing) {
            mplew.writeInt(ring2.getPartnerChrId());
            mplew.writeAsciiString(ring2.getPartnerName(), 13);
            mplew.writeLong((long) ring2.getRingId());
            mplew.writeLong((long) ring2.getPartnerRingId());
            mplew.writeInt(ring2.getItemId());
        }
        mplew.writeShort((int) (short) (short) ((chr.getMarriageRing(false) != null) ? 1 : 0));
        int marriageId = 30000;
        if (chr.getMarriageRing(false) != null) {
            mplew.writeInt(0);
            mplew.writeAsciiString("", 13);
            mplew.writeInt(chr.getId());
            mplew.writeInt(chr.getMarriageRing(false).getPartnerRingId());
        }
    }

    public static void addInventoryInfo(MaplePacketLittleEndianWriter mplew, MapleCharacter chr) {
        mplew.writeInt(chr.getMeso());
        mplew.writeInt(chr.getId());
        mplew.writeInt(chr.getBeans());
        mplew.writeInt(0);
        mplew.write(chr.getInventory(MapleInventoryType.EQUIP).getSlotLimit());
        mplew.write(chr.getInventory(MapleInventoryType.USE).getSlotLimit());
        mplew.write(chr.getInventory(MapleInventoryType.SETUP).getSlotLimit());
        mplew.write(chr.getInventory(MapleInventoryType.ETC).getSlotLimit());
        mplew.write(chr.getInventory(MapleInventoryType.CASH).getSlotLimit());
        mplew.writeLong(getTime(-2L));
        MapleInventory iv = chr.getInventory(MapleInventoryType.EQUIPPED);
        Collection<IItem> equippedC = iv.list();
        List<Item> equipped = (List<Item>) new ArrayList(equippedC.size());
        for (IItem item : equippedC) {
            equipped.add((Item) item);
        }
        Collections.sort(equipped);
        for (Item item2 : equipped) {
            if (item2.getPosition() < 0 && item2.getPosition() > -100) {
                addItemInfo(mplew, (IItem) item2, false, false);
            }
        }
        mplew.write(0);
        for (Item item2 : equipped) {
            if (item2.getPosition() <= -100 && item2.getPosition() > -1000) {
                addItemInfo(mplew, (IItem) item2, false, false);
            }
        }
        mplew.write(0);
        iv = chr.getInventory(MapleInventoryType.EQUIP);
        for (IItem item : iv.list()) {
            addItemInfo(mplew, item, false, false);
        }
        mplew.write(0);
        iv = chr.getInventory(MapleInventoryType.USE);
        for (IItem item : iv.list()) {
            addItemInfo(mplew, item, false, false);
        }
        mplew.write(0);
        iv = chr.getInventory(MapleInventoryType.SETUP);
        for (IItem item : iv.list()) {
            addItemInfo(mplew, item, false, false);
        }
        mplew.write(0);
        iv = chr.getInventory(MapleInventoryType.ETC);
        for (IItem item : iv.list()) {
            addItemInfo(mplew, item, false, false);
        }
        mplew.write(0);
        iv = chr.getInventory(MapleInventoryType.CASH);
        for (IItem item : iv.list()) {
            addItemInfo(mplew, item, false, false);
        }
        mplew.write(0);
    }

    public static void addCharStats(MaplePacketLittleEndianWriter mplew, MapleCharacter chr) {
        mplew.writeInt(chr.getId());
        mplew.writeAsciiString(chr.getName(), 13);
        mplew.write(chr.getGender());
        mplew.write(chr.getSkinColor());
        mplew.writeInt(chr.getFace());
        mplew.writeInt(chr.getHair());
        mplew.writeZeroBytes(24);
        mplew.write((int) chr.getLevel());
        mplew.writeShort((int) chr.getJob());
        chr.getStat().connectData(mplew);
        mplew.writeShort((int) chr.getRemainingAp());
        mplew.writeShort(chr.getRemainingSp());
        mplew.writeInt(chr.getExp());
        mplew.writeShort((int) chr.getFame());
        mplew.writeInt(0);
        mplew.writeLong(getTime(System.currentTimeMillis()));
        mplew.writeInt(chr.getMapId());
        mplew.write(chr.getInitialSpawnpoint());
    }

    public static void addCharLook(MaplePacketLittleEndianWriter mplew, MapleCharacter chr, boolean mega) {
        mplew.write(chr.getGender());
        mplew.write(chr.getSkinColor());
        mplew.writeInt(chr.getFace());
        mplew.write((int) (mega ? 0 : 1));
        mplew.writeInt(chr.getHair());
        Map<Byte, Integer> myEquip = (Map<Byte, Integer>) new LinkedHashMap();
        Map<Byte, Integer> maskedEquip = (Map<Byte, Integer>) new LinkedHashMap();
        MapleInventory equip = chr.getInventory(MapleInventoryType.EQUIPPED);
        for (IItem item : equip.list()) {
            if (item.getPosition() < -128) {
                continue;
            }
            byte pos = (byte) (item.getPosition() * -1);
            if (pos < 100 && myEquip.get(Byte.valueOf(pos)) == null) {
                myEquip.put(Byte.valueOf(pos), Integer.valueOf(item.getItemId()));
            } else if ((pos > 100 || pos == -128) && pos != 111) {
                pos = (byte) ((pos == -128) ? 28 : (pos - 100));
                if (myEquip.get(Byte.valueOf(pos)) != null) {
                    maskedEquip.put(Byte.valueOf(pos), myEquip.get(Byte.valueOf(pos)));
                }
                myEquip.put(Byte.valueOf(pos), Integer.valueOf(item.getItemId()));
            } else {
                if (myEquip.get(Byte.valueOf(pos)) == null) {
                    continue;
                }
                maskedEquip.put(Byte.valueOf(pos), Integer.valueOf(item.getItemId()));
            }
        }
        for (Entry<Byte, Integer> entry : myEquip.entrySet()) {
            mplew.write(((Byte) entry.getKey()).byteValue());
            mplew.writeInt(((Integer) entry.getValue()).intValue());
        }
        mplew.write(255);
        for (Entry<Byte, Integer> entry : maskedEquip.entrySet()) {
            mplew.write(((Byte) entry.getKey()).byteValue());
            mplew.writeInt(((Integer) entry.getValue()).intValue());
        }
        mplew.write(255);
        IItem cWeapon = equip.getItem((short) (-111));
        mplew.writeInt((cWeapon != null) ? cWeapon.getItemId() : 0);
        mplew.writeInt(0);
        mplew.writeLong(0L);
    }

    public static void addExpirationTime1(MaplePacketLittleEndianWriter mplew, long time) {
        mplew.writeLong((time > -1L) ? KoreanDateUtil.getFileTimestamp(time) : getTime(time));
    }

    public static void addExpirationTime(MaplePacketLittleEndianWriter mplew, long time) {
        long sc = 25200000L;
        mplew.write(0);
        mplew.writeShort(1408);
        if (time != -1L) {
            mplew.writeInt(KoreanDateUtil.getItemTimestamp(time + sc));
            mplew.write(1);
        } else {
            mplew.writeInt(400967355);
            mplew.write(2);
        }
    }

    public static void addItemInfo(MaplePacketLittleEndianWriter mplew, IItem item, boolean zeroPosition) {
        addItemInfo(mplew, item, zeroPosition);
    }

    public static void addItemInfo(MaplePacketLittleEndianWriter mplew, IItem item, boolean zeroPosition, boolean leaveOut) {
        MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
        boolean isPet = item.getPet() != null && item.getPet().getUniqueId() > -1;
        boolean isRing = false;
        boolean hasUniqueId = item.getUniqueId() > 0 && !GameConstants.isMarrigeRing(item.getItemId()) && item.getItemId() / 10000 != 166;
        Equip equip = null;
        short pos = item.getPosition();
        if (item.getType() == 1) {
            equip = (Equip) item;
            isRing = (equip.getRing() != null && equip.getRing().getRingId() > -1);
        }
        if (!zeroPosition) {
            if (equip != null) {
                if (pos < 0) {
                    pos *= -1;
                }
                mplew.write((pos > 100) ? (pos - 100) : pos);
            } else {
                mplew.write((int) pos);
            }
        }
        mplew.write((byte) ((item.getPet() != null) ? 3 : item.getType()));
        mplew.writeInt(item.getItemId());
        if (ii.isCash(item.getItemId()) && !isPet && item.getUniqueId() < 0) {
            int uniqueid = MapleItemInformationProvider.getUniqueId(item.getItemId(), null);
            item.setUniqueId(uniqueid);
        }
        mplew.write((int) (hasUniqueId ? 1 : 0));
        if (hasUniqueId) {
            if (isPet) {
                mplew.writeLong((long) item.getPet().getUniqueId());
            } else if (isRing) {
                mplew.writeLong((long) item.getRing().getRingId());
            } else {
                mplew.writeLong((long) item.getUniqueId());
            }
        }
        if (item.getPet() != null) {
            addPetItemInfo(mplew, item, item.getPet());
        } else {
            addExpirationTime(mplew, item.getExpiration());
            if (item.getType() == 1 && equip != null) {
                mplew.write(equip.getUpgradeSlots());
                mplew.write(equip.getLevel());
                mplew.writeShort((int) equip.getStr());
                mplew.writeShort((int) equip.getDex());
                mplew.writeShort((int) equip.getInt());
                mplew.writeShort((int) equip.getLuk());
                mplew.writeShort((int) equip.getHp());
                mplew.writeShort((int) equip.getMp());
                mplew.writeShort((int) equip.getWatk());
                mplew.writeShort((int) equip.getMatk());
                mplew.writeShort((int) equip.getWdef());
                mplew.writeShort((int) equip.getMdef());
                mplew.writeShort((int) equip.getAcc());
                mplew.writeShort((int) equip.getAvoid());
                mplew.writeShort((int) equip.getHands());
                mplew.writeShort((int) equip.getSpeed());
                mplew.writeShort((int) equip.getJump());
                mplew.writeMapleAsciiString(equip.getOwner());
                mplew.writeShort((int) equip.getFlag());
                mplew.write(0);
                mplew.write(Math.max(equip.getBaseLevel(), equip.getEquipLevel()));
                mplew.writeInt(equip.getExpPercentage() * 100000);
                mplew.writeInt((int) equip.getViciousHammer());
                if (!hasUniqueId) {
                    mplew.writeLong((long) item.getUniqueId());
                }
                mplew.writeLong(getTime(-2L));
                mplew.writeInt(-1);
            } else {
                mplew.writeShort((int) item.getQuantity());
                mplew.writeMapleAsciiString(item.getOwner());
                mplew.writeShort((int) item.getFlag());
                if (GameConstants.isExpChair(item.getItemId())) {
                    mplew.writeInt(0);
                    mplew.writeInt(0);
                }
                if (GameConstants.isRechargable(item.getItemId())) {
                    mplew.writeInt(2);
                    mplew.writeShort(84);
                    mplew.write(0);
                    mplew.write(52);
                }
            }
        }
    }

    public static void addItemInfo2(MaplePacketLittleEndianWriter mplew, IItem item, boolean zeroPosition, boolean leaveOut, boolean trade) {
        MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
        boolean isCash = ii.isCash(item.getItemId());
        boolean isPet = item.getPet() != null && item.getPet().getUniqueId() > -1;
        boolean isRing = false;
        Equip equip = null;
        short pos = item.getPosition();
        if (item.getType() == 1) {
            equip = (Equip) item;
            isRing = (equip.getRing() != null && equip.getRing().getRingId() > -1);
        }
        if (!zeroPosition) {
            if (equip != null) {
                if (pos < 0) {
                    pos *= -1;
                }
                mplew.write((pos > 100) ? (pos - 100) : pos);
            } else {
                mplew.write((int) pos);
            }
        }
        mplew.write(item.getType());
        mplew.writeInt(item.getItemId());
        mplew.write((int) (isCash ? 1 : 0));
        if (isCash) {
            mplew.writeLong(isPet ? ((long) item.getPet().getUniqueId()) : (isRing ? ((long) equip.getRing().getRingId()) : ((long) item.getUniqueId())));
        }
        if (isPet) {
            addPetItemInfo(mplew, item, item.getPet());
        } else {
            addExpirationTime(mplew, item.getExpiration());
            if (equip == null) {
                mplew.writeShort((int) item.getQuantity());
                mplew.writeMapleAsciiString(item.getOwner());
                mplew.writeShort((int) item.getFlag());
                if (GameConstants.isRechargable(item.getItemId())) {
                    mplew.writeInt(2);
                    mplew.writeShort(84);
                    mplew.write(0);
                    mplew.write(52);
                }
                return;
            }
            mplew.write(equip.getUpgradeSlots());
            mplew.write(equip.getLevel());
            mplew.writeShort((int) equip.getStr());
            mplew.writeShort((int) equip.getDex());
            mplew.writeShort((int) equip.getInt());
            mplew.writeShort((int) equip.getLuk());
            mplew.writeShort((int) equip.getHp());
            mplew.writeShort((int) equip.getMp());
            mplew.writeShort((int) equip.getWatk());
            mplew.writeShort((int) equip.getMatk());
            mplew.writeShort((int) equip.getWdef());
            mplew.writeShort((int) equip.getMdef());
            mplew.writeShort((int) equip.getAcc());
            mplew.writeShort((int) equip.getAvoid());
            mplew.writeShort((int) equip.getHands());
            mplew.writeShort((int) equip.getSpeed());
            mplew.writeShort((int) equip.getJump());
            mplew.writeMapleAsciiString(equip.getOwner());
            mplew.writeShort((int) equip.getFlag());
            mplew.write(equip.getLevel());
            mplew.write(equip.getExpPercentage());
            mplew.writeInt(0);
            if (!isCash) {
                mplew.writeLong((long) item.getUniqueId());
            }
            mplew.writeLong(getTime(-2L));
            mplew.writeInt(-1);
        }
    }

    public static void addItemInfo3(MaplePacketLittleEndianWriter mplew, IItem item) {
        addItemInfo3(mplew, item, null);
    }

    public static void addItemInfo3(MaplePacketLittleEndianWriter mplew, IItem item, MapleCharacter chr) {
        mplew.write((byte) ((item.getPet() != null) ? 3 : item.getType()));
        mplew.writeInt(item.getItemId());
        boolean hasUniqueId = item.getUniqueId() > 0 && !GameConstants.isMarriageRing(item.getItemId()) && item.getItemId() / 10000 != 166;
        mplew.write((int) (hasUniqueId ? 1 : 0));
        if (hasUniqueId) {
            mplew.writeLong((long) item.getUniqueId());
        }
        if (item.getPet() != null) {
            addPetItemInfo(mplew, item, item.getPet());
        } else {
            addExpirationTime1(mplew, item.getExpiration());
            if (item.getType() == 1) {
                Equip equip = (Equip) item;
                mplew.write(equip.getUpgradeSlots());
                mplew.write(equip.getLevel());
                mplew.writeShort((int) equip.getStr());
                mplew.writeShort((int) equip.getDex());
                mplew.writeShort((int) equip.getInt());
                mplew.writeShort((int) equip.getLuk());
                mplew.writeShort((int) equip.getHp());
                mplew.writeShort((int) equip.getMp());
                mplew.writeShort((int) equip.getWatk());
                mplew.writeShort((int) equip.getMatk());
                mplew.writeShort((int) equip.getWdef());
                mplew.writeShort((int) equip.getMdef());
                mplew.writeShort((int) equip.getAcc());
                mplew.writeShort((int) equip.getAvoid());
                mplew.writeShort((int) equip.getHands());
                mplew.writeShort((int) equip.getSpeed());
                mplew.writeShort((int) equip.getJump());
                mplew.writeMapleAsciiString(equip.getOwner());
                mplew.writeShort((int) equip.getFlag());
                mplew.write((int) ((equip.getIncSkill() > 0) ? 1 : 0));
                mplew.write(Math.max(equip.getBaseLevel(), equip.getEquipLevel()));
                mplew.writeInt(equip.getExpPercentage() * 100000);
                mplew.writeInt((int) equip.getViciousHammer());
                if (!hasUniqueId) {
                    mplew.writeLong((equip.getInventoryId() <= 0L) ? -1L : equip.getInventoryId());
                }
                addExpirationTime1(mplew, item.getExpiration());
                mplew.writeInt(-1);
            } else {
                mplew.writeShort((int) item.getQuantity());
                mplew.writeMapleAsciiString(item.getOwner());
                mplew.writeShort((int) item.getFlag());
                if (GameConstants.isThrowingStar(item.getItemId()) || GameConstants.isBullet(item.getItemId()) || item.getItemId() / 10000 == 287) {
                    mplew.writeLong((item.getInventoryId() <= 0L) ? -1L : item.getInventoryId());
                }
            }
        }
    }

    public static void serializeMovementList(MaplePacketLittleEndianWriter lew, List<LifeMovementFragment> moves) {
        lew.write(moves.size());
        for (LifeMovementFragment move : moves) {
            move.serialize(lew);
        }
    }

    public static void addAnnounceBox(MaplePacketLittleEndianWriter mplew, MapleCharacter chr) {
        if (chr.getPlayerShop() != null && chr.getPlayerShop().isOwner(chr) && chr.getPlayerShop().getShopType() != 1 && chr.getPlayerShop().isAvailable()) {
            addInteraction(mplew, chr.getPlayerShop());
        } else {
            mplew.write(0);
        }
    }

    public static void addInteraction(MaplePacketLittleEndianWriter mplew, IMaplePlayerShop shop) {
        mplew.write(shop.getGameType());
        mplew.writeInt(((AbstractPlayerStore) shop).getObjectId());
        mplew.writeMapleAsciiString(shop.getDescription());
        if (shop.getShopType() != 1) {
            mplew.write((int) ((shop.getPassword().length() > 0) ? 1 : 0));
        }
        mplew.write(shop.getItemId() % 10);
        mplew.write(shop.getSize());
        mplew.write(shop.getMaxSize());
        if (shop.getShopType() != 1) {
            mplew.write((int) (shop.isOpen() ? 0 : 1));
        }
    }

    public static void addCharacterInfo(MaplePacketLittleEndianWriter mplew, MapleCharacter chr, boolean isCs) {
        mplew.writeLong(-1L);
        mplew.write(0);
        addCharStats(mplew, chr);
        mplew.write(chr.getBuddylist().getCapacity());
        if (!isCs) {
            if (chr.getBlessOfFairyOrigin() != null) {
                mplew.write(1);
                mplew.writeMapleAsciiString(chr.getBlessOfFairyOrigin());
            } else {
                mplew.write(0);
            }
        } else {
            mplew.write(0);
        }
        addInventoryInfo(mplew, chr);
        if (!isCs) {
            addSkillInfo(mplew, chr);
        } else {
            mplew.writeShort(0);
        }
        if (!isCs) {
            addCoolDownInfo(mplew, chr);
        } else {
            mplew.writeShort(0);
        }
        if (!isCs) {
            addQuestInfo(mplew, chr);
        } else {
            mplew.writeShort(0);
            mplew.writeShort(0);
        }
        addRingInfo(mplew, chr);
        addRocksInfo(mplew, chr);
        if (!isCs) {
            addMonsterBookInfo(mplew, chr);
        } else {
            mplew.writeInt(1);
            mplew.write(0);
            mplew.writeShort(0);
        }
        if (!isCs) {
            chr.QuestInfoPacket(mplew);
        } else {
            mplew.writeShort(0);
        }
        mplew.writeShort(0);
        mplew.writeShort(0);
        mplew.writeShort(0);
    }

    public static void addPetItemInfo(MaplePacketLittleEndianWriter mplew, IItem item, MaplePet pet) {
        addExpirationTime(mplew, (item != null) ? item.getExpiration() : -1L);
        String petname = pet.getName();
        if (petname == null) {
            petname = "";
        }
        mplew.writeAsciiString(petname, 13);
        mplew.write(pet.getLevel());
        mplew.writeShort((int) pet.getCloseness());
        mplew.write(pet.getFullness());
        if (item == null) {
            mplew.writeLong(getKoreanTimestamp((long) ((double) System.currentTimeMillis() * 1.5)));
        } else {
            addExpirationTime(mplew, (item.getExpiration() <= System.currentTimeMillis()) ? -1L : item.getExpiration());
        }
        mplew.writeShort(0);
        mplew.writeShort((int) pet.getFlags());
        mplew.writeShort(0);
        for (int i = 0; i < 4; ++i) {
            mplew.write(0);
        }
    }

    static {
        unk1 = new byte[]{0, 64, -32, -3};
        unk2 = new byte[]{59, 55, 79, 1};
    }
}
